#-*-encoding=utf-8-*-
@script_attr(permission='zope.View',use_template='basic')
def test():
    """        解密程序，需要传入用户名，密码，时间戳。返回解密后的密匙         

None"""

    import hashlib,time
    
    timeout = 10*60
    user_list = ['xtz','admin','guest','passwd','apple']
    user = request.get('username','')
    sign = request.get('sign','')
    time_stamp = request.get('timestamp','')
    sso_pwd = '123'
    
    def parse(user,sso_pwd,time_stamp):
        """
        解密程序，需要传入用户名，密码，时间戳。返回解密后的密匙
        """
        sign_md5 = hashlib.md5()
        sign_md5.update("%s%s%s" % (user, sso_pwd, time_stamp))
        sign = sign_md5.hexdigest()
        return sign
    
    if user not in user_list:
       return '用户不存在!'
    
    if not sign == parse(user,sso_pwd,time_stamp):
       return '密匙错误！'
    
    currentTime = int( time.time() )
    
    if currentTime  - int( time_stamp ) > timeout :
        return '你的登录信息已经过期了'
    else:
        # 这里要提示已经登录
        return request.response.redirect( request.get('came_from','') )
    

@script_attr(permission=u'zope.View',use_template='basic')
def index():
    """首页

这是应用的首页"""
    title = IDublinCore(context).title
    local_path = absoluteURL(context, request) + '/++script++zopen.iframe.do_forward'
    oa_column = appletsetting['oa_column']
    
    
    if oa_column is None or ( not oa_column ):
        return """您还没有设置，请设置成功后，再尝试使用!<br />
                  您可以在平台的定制模式，在左侧应用导航找到“内嵌帧集成器“，然后在右侧应用设置里设置。
               """
    
    result = []
    for one_column in oa_column:
        if not( one_column['column_name'] and one_column['column_url'] ):
            continue
        one_result = {}
        one_result['name'] = one_column['column_name']
        one_result['url'] = local_path + '?objurl=' + one_column['column_url']
        result.append(one_result)
    
    return render_template('oa_index', columns = result, first_url = result[0]['url'],title=title)

@script_attr(permission='zope.View',use_template='basic')
def do_forward():
    """转发处理

None"""

    ssologin_url = appletsetting['ssologin_url']
    sso_pwd = appletsetting['sso_pwd']
    obj_url = request.get('objurl','')
    
    if not ssologin_url:
       return request.response.redirect(obj_url)
    
    
    user = request.principal.id.split('.', 1)[1]
    sso_url = ssoURL(user, ssologin_url, sso_pwd, obj_url) 
    
    return request.response.redirect(sso_url)
    

